﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.LuFang.API.Application.Command.RoleMenuCommand;
using RBAC.LuFang.API.Dtos;
using RBAC.LuFang.Domain;
using RBAC.LuFang.Domain.DTO;
using RBAC.LuFang.Domin.Services;
using System.Security.Claims;
using static RBAC.LuFang.Domin.Services.RoleMenuService;

namespace RBAC.LuFang.API.Controllers
{
    /// <summary>
    /// 角色菜单
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleMenuController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IRoleMenuService _rolemenuService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public RoleMenuController(IMediator mediator, IRoleMenuService rolemenuService)
        {
            _mediator = mediator;
            _rolemenuService = rolemenuService;
        }

        /// <summary>
        /// 显示角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Menutun1>>> GetRole()
        {
            ApiResult<List<Menutun1>> result=new ApiResult<List<Menutun1>>();
            try
            {
                result.Data = await _mediator.Send(new RolesSelectCommand());
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示权限菜单 使用递归查询子集
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Menutun1>>> GetMenu()
        {
            ApiResult<List<Menutun1>> result = new ApiResult<List<Menutun1>>();
            try
            {
                result.Data= await _mediator.Send(new RoleMenuSelectCommand());
            }
            catch (Exception ee)
            {
                result.Code= -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 根据角色id反填菜单
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<List<FanMenuDTO>>> FanMenu(RoleMenuFanCommand rid)
        {
            ApiResult<List<FanMenuDTO>> result = new ApiResult<List<FanMenuDTO>>();
            try
            {
                result.Data = await _mediator.Send(rid);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 角色菜单添加
        /// </summary>
        /// <param name="rid"></param>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddRoleMenu(RoleMenuCreateCommand role)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(role);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示菜单（没有用递归）
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ApiResult<List<GetMenu1DTO>>> GetMenu1()
        {
            ApiResult<List<GetMenu1DTO>> result = new ApiResult<List<GetMenu1DTO>>();
            try
            {
                var id = Convert.ToInt32(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
                result.Data= await _rolemenuService.GetMenu1(id);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }



    }
}
